package com.xiaodao.springboot.config;


import com.xiaodao.springboot.common.BaseErrorInfoInterface;
import lombok.Data;

import java.util.HashMap;

@Data
public final class ApiResponse {

  private Integer code;
  private String errorMsg;
  private Object data;

  public ApiResponse() {
  }

  public ApiResponse(Integer code, String errorMsg, Object data) {
    this.code = code;
    this.errorMsg = errorMsg;
    this.data = data;
  }

  public ApiResponse(BaseErrorInfoInterface errorInfo) {
    this.code = errorInfo.getResultCode();
    this.errorMsg = errorInfo.getResultMsg();
  }

  public static ApiResponse ok() {
    return new ApiResponse(200, "", new HashMap<>());
  }

  public static ApiResponse ok(String errorMsg) {
    return new ApiResponse(200, errorMsg, new HashMap<>());
  }

  public static ApiResponse ok(Object data) {
    return new ApiResponse(200, "成功", data);
  }

  public static ApiResponse ok(Integer code,Object data) {
    return new ApiResponse(code, "", data);
  }


  /**
   * 失败
   */
  public static ApiResponse error(BaseErrorInfoInterface errorInfo) {
    return new ApiResponse(errorInfo.getResultCode(), errorInfo.getResultMsg(), null);
  }

  /**
   * 失败
   */
  public static ApiResponse error(Integer code, String message) {
    ApiResponse rb = new ApiResponse();
    rb.setCode(code);
    rb.setErrorMsg(message);
    rb.setData(null);
    return rb;
  }

  /**
   *@Description: 失败并返回错误所需的部分信息
   *@Author: xuxiaodong
   *@Date: 2024/7/30
  */
  public static ApiResponse error(Integer code, String message,Object data) {
    ApiResponse rb = new ApiResponse();
    rb.setCode(code);
    rb.setErrorMsg(message);
    rb.setData(data);
    return rb;
  }
}
